﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.DataModel.TestItem;
using Bymed.SpineScan.Service;
using Bymed.SpineScan.Service.Interface;
using Bymed.SpineScan.Service.Shared;
using Bymed.SpineScan.WPF.ViewModel;
using Bymed.SpineScan.WPF.ViewModel.TestItemViewModel;

namespace Bymed.SpineScan.WPF.Shared
{
    /// <summary>
    /// Provide some shared methods used within ViewModel or for ViewModel
    /// </summary>
    public static class ViewModelHelper
    {
        public const string TestItemLateralBendingTypeTitle = "侧向弯曲";
        public const string TestItemFlexExtTypeTitle = "前屈后伸";
        public const string TestItemRotationTypeTitle = "水平旋转";
        public const string TestItemBalanceTypeTitle = "身体平衡";
        public const string TestItemLateralBendingTypeName = "LateralBending";
        public const string TestItemFlexExtTypeName = "FlexExt";
        public const string TestItemRotationTypeName = "Rotation";
        public const string TestItemBalanceTypeName = "Balance";

        /// <summary>
        /// Copy all test item values from one AllTestItemsAndPatientInfoViewModel to another.
        /// </summary>
        /// <param name="source">An instance of AllTestItemsAndPatientInfoViewModel contains all test items.</param>
        /// <param name="target">An instance of AllTestItemsAndPatientInfoViewModel accepts all test items.</param>
        public static void CopyAllTestItemsViewModel(AllTestItemsViewModel source, AllTestItemsViewModel target)
        {
            // For test summary report ONLY
            CopySingleTestItem(source.ScoliosisATI, target.ScoliosisATI);
            CopySingleTestItem(source.Kyphosis, target.Kyphosis);
            CopySingleTestItem(source.CervicalFlexExt, target.CervicalFlexExt);
            CopySingleTestItem(source.ThoracicFlexExt, target.ThoracicFlexExt);
            CopySingleTestItem(source.LumbarFlexExt, target.LumbarFlexExt);
            CopySingleTestItem(source.CervicalLateralBending, target.CervicalLateralBending);
            CopySingleTestItem(source.ThoracicLateralBending, target.ThoracicLateralBending);
            CopySingleTestItem(source.LumbarLateralBending, target.LumbarLateralBending);
            CopySingleTestItem(source.CervicalRotation, target.CervicalRotation);
            CopySingleTestItem(source.ThoracicRotation, target.ThoracicRotation);
            CopySingleTestItem(source.LumbarRotation, target.LumbarRotation);
            CopySingleTestItem(source.HeadBalance, target.HeadBalance);
            CopySingleTestItem(source.ShouldersBalance, target.ShouldersBalance);
            CopySingleTestItem(source.PelvicBalance, target.PelvicBalance);

            target.GenerateSelectedTestItemList();

            SingleTestItemViewModel existingItem;
            int existingItemIndex = -1;

            foreach (var item in source.TestItemList)
            {
                existingItem = target.TestItemList.FirstOrDefault(p => p.TestItemName == item.TestItemName);

                if (existingItem == null)
                {
                    target.TestItemList.Add(CopySingleTestItem(item, null));
                }
                else
                {
                    existingItemIndex = target.TestItemList.IndexOf(existingItem);
                    target.TestItemList.Remove(existingItem);
                    target.TestItemList.Insert(existingItemIndex, CopySingleTestItem(item, null));
                }
            }

            target.SelectedCustomizedTestItemList = source.SelectedCustomizedTestItemList;
        }

        /// <summary>
        /// Sync the items in TestItemList with all TestItem related properties.
        /// </summary>
        /// <param name="source">The instance of AllTestItemsViewModel.</param>
        public static void SyncTestItemListToProperties(AllTestItemsViewModel source)
        {
            foreach (var item in source.TestItemList)
            {
                if (!item.IsCustomizedTestItem)
                {
                    if (item is TestItemScoliosisATIViewModel)
                    {
                        CopySingleTestItem(item, source.ScoliosisATI);
                    }
                    else if (item is TestItemKyphosisViewModel)
                    {
                        CopySingleTestItem(item, source.Kyphosis);
                    }
                    else if (item is TestItemCervicalFlexionExtensionViewModel)
                    {
                        CopySingleTestItem(item, source.CervicalFlexExt);
                    }
                    else if (item is TestItemThoracicFlexionExtensionViewModel)
                    {
                        CopySingleTestItem(item, source.ThoracicFlexExt);
                    }
                    else if (item is TestItemLumbarFlexionExtensionViewModel)
                    {
                        CopySingleTestItem(item, source.LumbarFlexExt);
                    }
                    else if (item is TestItemCervicalLateralBendingViewModel)
                    {
                        CopySingleTestItem(item, source.CervicalLateralBending);
                    }
                    else if (item is TestItemThoracicLateralBendingViewModel)
                    {
                        CopySingleTestItem(item, source.ThoracicLateralBending);
                    }
                    else if (item is TestItemLumbarLateralBendingViewModel)
                    {
                        CopySingleTestItem(item, source.LumbarLateralBending);
                    }
                    else if (item is TestItemCervicalRotationViewModel)
                    {
                        CopySingleTestItem(item, source.CervicalRotation);
                    }
                    else if (item is TestItemThoracicRotationViewModel)
                    {
                        CopySingleTestItem(item, source.ThoracicRotation);
                    }
                    else if (item is TestItemLumbarRotationViewModel)
                    {
                        CopySingleTestItem(item, source.LumbarRotation);
                    }
                    else if (item is TestItemHeadBalanceViewModel)
                    {
                        CopySingleTestItem(item, source.HeadBalance);
                    }
                    else if (item is TestItemShouldersBalanceViewModel)
                    {
                        CopySingleTestItem(item, source.ShouldersBalance);
                    }
                    else if (item is TestItemPelvicBalanceViewModel)
                    {
                        CopySingleTestItem(item, source.PelvicBalance);
                    }
                }
            }
        }

        /// <summary>
        /// Copy one single test item value from one SingleTestItemViewModel to another.
        /// </summary>
        /// <param name="source">An instance of SingleTestItemViewModel contains test values.</param>
        /// <param name="target">An instance of SingleTestItemViewModel accepts test values.</param>
        /// <returns>If the target is null, then the created instance of target will be returned.</returns>
        public static SingleTestItemViewModel CopySingleTestItem(SingleTestItemViewModel source, SingleTestItemViewModel target)
        {
            if (target == null)
            {
                target = GetSingleTestItemViewModelInstance(source);
            }

            target.FirstMaxValue = source.FirstMaxValue;
            target.FirstRangeEndValue = source.FirstRangeEndValue;
            target.FirstRangeStartValue = source.FirstRangeStartValue;
            target.FirstTestResult = source.FirstTestResult;
            target.FirstTestResultLabel = source.FirstTestResultLabel;
            target.FormattedTestResult = source.FormattedTestResult;
            target.IsReadyToTest = source.IsReadyToTest;
            target.IsSelected = source.IsSelected;
            target.NeedDetailReport = source.NeedDetailReport;
            target.ResultRawData = source.ResultRawData;
            target.ReverseBackgroundColor = source.ReverseBackgroundColor;
            target.SecondMaxValue = source.SecondMaxValue;
            target.SecondRangeEndValue = source.SecondRangeEndValue;
            target.SecondRangeStartValue = source.SecondRangeStartValue;
            target.SecondTestResult = source.SecondTestResult;
            target.SecondTestResultLabel = source.SecondTestResultLabel;
            target.SequenceNumber = source.SequenceNumber;
            target.TestDateDisplay = source.TestDateDisplay;
            target.TestItemName = source.TestItemName;
            target.TestItemTitle = source.TestItemTitle;
            target.TestPositionName = source.TestPositionName;
            target.TestItemType = source.TestItemType;
            target.TestItemTypeDescription = source.TestItemTypeDescription;
            target.IsCustomizedTestItem = source.IsCustomizedTestItem;

            target.GraphBaseLineData = source.GraphBaseLineData;
            target.GraphData = source.GraphData;

            return target;
        }

        /// <summary>
        /// Copy all test item, attending doctor & patient display references from one AllTestItemsAndPatientInfoViewModel to another.
        /// </summary>
        /// <param name="source">An instance of AllTestItemsAndPatientInfoViewModel contains all test items.</param>
        /// <param name="target">An instance of AllTestItemsAndPatientInfoViewModel accepts all test items.</param>
        public static void CopyTestItemAndAllRelatedInfoReference(AllTestItemsAndPatientInfoViewModel source, AllTestItemsAndPatientInfoViewModel target)
        {
            CopyAllTestItemsViewModel(source, target);
            target.PatientBasicInfoDisplayViewModel = source.PatientBasicInfoDisplayViewModel;
            target.AttendingDoctor = source.AttendingDoctor;
        }

        /// <summary>
        /// Create a specified type of test item based on provided instance inherits from SingleTestItemViewModel.
        /// </summary>
        /// <param name="testItem">The provided instance inherits from SingleTestItemViewModel.</param>
        /// <returns>A specified type of test item inherits from SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel GetSingleTestItemViewModelInstance(SingleTestItemViewModel testItem)
        {
            return (SingleTestItemViewModel)Activator.CreateInstance(testItem.GetType(), new object[1] { null });
        }

        /// <summary>
        /// Create a specified type of test item based on provided test item name.
        /// </summary>
        /// <param name="testItem">The provided test item name.</param>
        /// <returns>A specified type of test item inherits from SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel GetSingleTestItemViewModelInstance(string testItemName)
        {
            SingleTestItemViewModel item = null;

            switch (testItemName)
            {
                case TestItemDefinition.ScoliosisATITestName:
                    item = new TestItemScoliosisATIViewModel(null);
                    break;
                case TestItemDefinition.KyphosisTestName:
                    item = new TestItemKyphosisViewModel(null);
                    break;
                case TestItemDefinition.CervicalLateralBendingTestName:
                    item = new TestItemCervicalLateralBendingViewModel(null);
                    break;
                case TestItemDefinition.ThoracicLateralBendingTestName:
                    item = new TestItemThoracicLateralBendingViewModel(null);
                    break;
                case TestItemDefinition.LumbarLateralBendingTestName:
                    item = new TestItemLumbarLateralBendingViewModel(null);
                    break;
                case TestItemDefinition.CervicalFlexExtTestName:
                    item = new TestItemCervicalFlexionExtensionViewModel(null);
                    break;
                case TestItemDefinition.ThoracicFlexExtTestName:
                    item = new TestItemThoracicFlexionExtensionViewModel(null);
                    break;
                case TestItemDefinition.LumbarFlexExtTestName:
                    item = new TestItemLumbarFlexionExtensionViewModel(null);
                    break;
                case TestItemDefinition.CervicalRotationTestName:
                    item = new TestItemCervicalRotationViewModel(null);
                    break;
                case TestItemDefinition.ThoracicRotationTestName:
                    item = new TestItemThoracicRotationViewModel(null);
                    break;
                case TestItemDefinition.LumbarRotationTestName:
                    item = new TestItemLumbarRotationViewModel(null);
                    break;
                case TestItemDefinition.HeadBalanceTestName:
                    item = new TestItemHeadBalanceViewModel(null);
                    break;
                case TestItemDefinition.ShouldersBalanceTestName:
                    item = new TestItemShouldersBalanceViewModel(null);
                    break;
                case TestItemDefinition.PelvicBalanceTestName:
                    item = new TestItemPelvicBalanceViewModel(null);
                    break;
            }

            return item;
        }

        /// <summary>
        /// Initialize all test properties for AllTestResultsViewModel.
        /// </summary>
        /// <param name="allTestResultsViewModel">The instance of AllTestResultsViewModel.</param>
        /// <param name="testTracking">The instance of TestTracking.</param>
        /// <returns>The instance of AllTestResultsViewModel with all initialized properties.</returns>
        public static AllTestResultsViewModel InitializeAllTestProperties(AllTestResultsViewModel allTestResultsViewModel, TestTracking testTracking)
        {
            // ScoliosisATI
            allTestResultsViewModel.ScoliosisATI = (TestItemScoliosisATIViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.ScoliosisATI.TestItemName) ?? new TestItemScoliosisATIViewModel(null);

            // Kyphosis
            allTestResultsViewModel.Kyphosis = (TestItemKyphosisViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.Kyphosis.TestItemName) ?? new TestItemKyphosisViewModel(null);

            // LateralBending
            allTestResultsViewModel.CervicalLateralBending = (TestItemCervicalLateralBendingViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.CervicalLateralBending.TestItemName) ?? new TestItemCervicalLateralBendingViewModel(null);

            allTestResultsViewModel.ThoracicLateralBending = (TestItemThoracicLateralBendingViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.ThoracicLateralBending.TestItemName) ?? new TestItemThoracicLateralBendingViewModel(null);

            allTestResultsViewModel.LumbarLateralBending = (TestItemLumbarLateralBendingViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.LumbarLateralBending.TestItemName) ?? new TestItemLumbarLateralBendingViewModel(null);

            // Flexion & Extension
            allTestResultsViewModel.CervicalFlexExt = (TestItemCervicalFlexionExtensionViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.CervicalFlexExt.TestItemName) ?? new TestItemCervicalFlexionExtensionViewModel(null);

            allTestResultsViewModel.ThoracicFlexExt = (TestItemThoracicFlexionExtensionViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.ThoracicFlexExt.TestItemName) ?? new TestItemThoracicFlexionExtensionViewModel(null);

            allTestResultsViewModel.LumbarFlexExt = (TestItemLumbarFlexionExtensionViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.LumbarFlexExt.TestItemName) ?? new TestItemLumbarFlexionExtensionViewModel(null);

            // Rotation
            allTestResultsViewModel.CervicalRotation = (TestItemCervicalRotationViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.CervicalRotation.TestItemName) ?? new TestItemCervicalRotationViewModel(null);

            allTestResultsViewModel.ThoracicRotation = (TestItemThoracicRotationViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.ThoracicRotation.TestItemName) ?? new TestItemThoracicRotationViewModel(null);

            allTestResultsViewModel.LumbarRotation = (TestItemLumbarRotationViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.LumbarRotation.TestItemName) ?? new TestItemLumbarRotationViewModel(null);

            // Body Balance
            allTestResultsViewModel.HeadBalance = (TestItemHeadBalanceViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.HeadBalance.TestItemName) ?? new TestItemHeadBalanceViewModel(null);

            // ShouldersBalance
            allTestResultsViewModel.ShouldersBalance = (TestItemShouldersBalanceViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.ShouldersBalance.TestItemName) ?? new TestItemShouldersBalanceViewModel(null);
            // PelvicBalance
            allTestResultsViewModel.PelvicBalance = (TestItemPelvicBalanceViewModel)ViewModelHelper.ConvertFromTestResults(testTracking.TestResults,
                            allTestResultsViewModel.PelvicBalance.TestItemName) ?? new TestItemPelvicBalanceViewModel(null);

            return allTestResultsViewModel;
        }

        /// <summary>
        /// Convert from TestResult collection based on provided test item name.
        /// </summary>
        /// <param name="testResults">The TestResult collection of the TestResult.</param>
        /// <param name="testItemName">Test item name.</param>
        /// <returns>The instance of SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel ConvertFromTestResults(ICollection<TestResult> testResults, string testItemName)
        {
            var matchedTestResult = (from testResult in testResults
                                     where testResult.TestItemName == testItemName
                                     select testResult).FirstOrDefault();

            if (matchedTestResult != null)
            {
                return ViewModelHelper.ConvertFromTestResult(matchedTestResult, testItemName);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Convert from TestResult collection based on provided test item.
        /// </summary>
        /// <param name="testResults">The TestResult collection of the TestResult.</param>
        /// <param name="testItem">The test item.</param>
        /// <returns>The instance of SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel ConvertFromTestResults(ICollection<TestResult> testResults, SingleTestItemViewModel testItem)
        {
            if (!GeneralUtility.IsPositiveInteger(testItem.TestItemName))
            {
                return ConvertFromTestResults(testResults, testItem.TestItemName);
            }
            else
            {
                var matchedTestResult = (from testResult in testResults
                                         where testResult.TestItemName == testItem.TestItemName
                                         select testResult).FirstOrDefault();

                if (matchedTestResult != null)
                {
                    var newTestItem = GetSingleTestItemViewModelInstance(testItem);
                    return ViewModelHelper.ConvertFromTestResult(matchedTestResult, newTestItem);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Convert from TestResult to SingleTestItemViewModel and assign the value for all necessary properties.
        /// </summary>
        /// <param name="testResult">The instance of TestResult.</param>
        /// <param name="testItemName">Test item name.</param>
        /// <returns>The instance of SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel ConvertFromTestResult(TestResult testResult, string testItemName)
        {
            SingleTestItemViewModel singleTestItemViewModel = ViewModelHelper.GetSingleTestItemViewModelInstance(testItemName);

            return ConvertFromTestResult(testResult, singleTestItemViewModel);
        }

        /// <summary>
        /// Convert from TestResult to SingleTestItemViewModel and assign the value for all necessary properties.
        /// </summary>
        /// <param name="testResult">The instance of TestResult.</param>
        /// <param name="SingleTestItemViewModel">Created instance of singleTestItemViewModel.</param>
        /// <returns>The instance of SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel ConvertFromTestResult(TestResult testResult, SingleTestItemViewModel singleTestItemViewModel)
        {
            var testItem = TestItemFactory.GetTestItem(testResult.ResultRawData);

            if (testItem is ScoliosisATITest)
            {
                singleTestItemViewModel.SetupTestPositionName(testItem.TestPosition);
                singleTestItemViewModel.GerenateGraphData(TestItemFactory.ConvertGraphRawData(testResult.Graph));
            }

            singleTestItemViewModel.FirstTestResult = testItem.FirstResult;
            singleTestItemViewModel.SecondTestResult = testItem.SecondResult;
            singleTestItemViewModel.NeedDetailReport = true;
            singleTestItemViewModel.IsSelected = true;
            ConvertTestResultBasedOnTestType(singleTestItemViewModel);
            singleTestItemViewModel.SetFormattedTestResult();

            return singleTestItemViewModel;
        }

        /// <summary>
        /// User might test only one position for LateralBending, FlexionExtension & Rotation.
        /// If system finds the test item is matched one of them and the test result is 0, then 
        /// this test result will be considered as -1.
        /// </summary>
        /// <param name="testItem">Instance of SingleTestItemViewModel contains all test result.</param>
        public static void ConvertTestResultBasedOnTestType(SingleTestItemViewModel testItem)
        {
            if (testItem is TestItemCervicalLateralBendingViewModel ||
                testItem is TestItemThoracicLateralBendingViewModel ||
                testItem is TestItemLumbarLateralBendingViewModel ||
                testItem is TestItemCervicalFlexionExtensionViewModel ||
                testItem is TestItemThoracicFlexionExtensionViewModel ||
                testItem is TestItemLumbarFlexionExtensionViewModel ||
                testItem is TestItemCervicalRotationViewModel ||
                testItem is TestItemThoracicRotationViewModel ||
                testItem is TestItemLumbarRotationViewModel)
            {
                if (testItem.FirstTestResult != 0 || testItem.SecondTestResult != 0)
                {
                    if (testItem.FirstTestResult == 0)
                    {
                        testItem.FirstTestResult = -1;
                    }

                    if (testItem.SecondTestResult == 0)
                    {
                        testItem.SecondTestResult = -1;
                    }
                }
            }
        }

        /// <summary>
        /// Reset the specified SingleTestItemViewModel properties to default value.
        /// </summary>
        /// <param name="testItem"></param>
        public static void ResetSingleTestItemViewModel(SingleTestItemViewModel testItem)
        {
            testItem.IsSelected = false;
            testItem.IsReadyToTest = false;
            testItem.NeedDetailReport = false;
            ResetSingleTestItemTestResult(testItem);
        }

        /// <summary>
        /// Reset the test result properties of provided SingleTestItemViewModel.
        /// </summary>
        /// <param name="testItem"></param>
        public static void ResetSingleTestItemTestResult(SingleTestItemViewModel testItem)
        {
            testItem.FirstTestResult = null;
            testItem.SecondTestResult = null;
            testItem.FormattedTestResult = string.Empty;
            testItem.GraphData.Clear();
        }

        /// <summary>
        /// Load TestResult data based on provided SingleTestItemViewModel.
        /// </summary>
        /// <param name="testTrackingService">Instance of a class that implements ITestTrackingService.</param>
        /// <param name="testItem">Instance of SingleTestItemViewModel class.</param>
        /// <param name="patientNumber">The patient number.</param>
        /// <param name="rowCount">How many rows will be returned.</param>
        /// <returns>A collection of SingleTestItemViewModel.</returns>
        public static IList<SingleTestItemViewModel> LoadTestResultData(ITestTrackingService testTrackingService,
            SingleTestItemViewModel testItem, string patientNumber, int? rowCount)
        {
            var singleTestItemViewModel = ViewModelHelper.GetSingleTestItemViewModelInstance(testItem);
            singleTestItemViewModel.TestItemName = testItem.TestItemName;

            var testTrackings = testTrackingService.GetAllRelatedTestResultsByPatientNumber(patientNumber, singleTestItemViewModel.TestItemName, rowCount);
            var singleTestItemList = new List<SingleTestItemViewModel>();

            for (int i = 0; i < testTrackings.Count; i++)
            {
                var singleTestItem = ViewModelHelper.ConvertFromTestResults(testTrackings[i].TestResults, singleTestItemViewModel);
                singleTestItem.TestItemTitle = testItem.TestItemTitle;
                singleTestItem.TestItemTypeDescription = testItem.TestItemTypeDescription;
                singleTestItem.TestDateDisplay = testTrackings[i].TestDate.ToString(GeneralUtility.LongDateTimeFormat);
                singleTestItem.SequenceNumber = i + 1;

                singleTestItemList.Add(singleTestItem);
            }

            return singleTestItemList;
        }

        /// <summary>
        /// Create an instance of SingleTestItemViewModel based on proivded instance of CustomizedTestItem.
        /// </summary>
        /// <param name="item">An instance of CustomizedTestItem.</param>
        /// <returns>An instance of SingleTestItemViewModel.</returns>
        public static SingleTestItemViewModel CreateCustomizedTestItemViewModel(CustomizedTestItem item)
        {
            SingleTestItemViewModel viewModel = null;

            switch (item.ItemType)
            {
                case TestItemLateralBendingTypeName:
                    viewModel = new TestItemCervicalLateralBendingViewModel(null);
                    viewModel.TestItemTypeDescription = TestItemLateralBendingTypeTitle;
                    break;
                case TestItemFlexExtTypeName:
                    viewModel = new TestItemCervicalFlexionExtensionViewModel(null);
                    viewModel.TestItemTypeDescription = TestItemFlexExtTypeTitle;
                    break;
                case TestItemRotationTypeName:
                    viewModel = new TestItemCervicalRotationViewModel(null);
                    viewModel.TestItemTypeDescription = TestItemRotationTypeTitle;
                    break;
                case TestItemBalanceTypeName:
                    viewModel = new TestItemHeadBalanceViewModel(null);
                    viewModel.TestItemTypeDescription = TestItemBalanceTypeTitle;
                    break;
            }

            viewModel.TestItemTitle = item.ItemTitle;
            viewModel.TestItemName = item.ItemTimestamp;
            viewModel.TestItemType = item.ItemType;
            viewModel.IsSelected = true;
            viewModel.IsCustomizedTestItem = true;

            return viewModel;
        }
    }
}
